package com.stylefeng.guns.modular.fileManage.controller;

import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.ZyRecycleMapper;
import com.stylefeng.guns.common.persistence.model.ZyRecycle;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.modular.fileManage.dao.ZyRecycleDao;
import com.stylefeng.guns.modular.fileManage.service.IZyRecycleService;
import com.stylefeng.guns.modular.fileManage.service.impl.ZyRecycleServiceImpl;
import com.stylefeng.guns.modular.fileManage.warpper.RecycleWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zengyongfeng
 * @description 用于系统回收站功能的控制器（茶叶资料、冲泡方式、用户等）
 * @date 2017-12-21
 */
@Controller
@RequestMapping("/recycle")
public class RecycleController extends BaseController {

    private String PREFIX = "/fileManage/recycle/";

    @Autowired
    private ZyRecycleServiceImpl zyRecycleServiceImpl;

    @Autowired
    private ZyRecycleMapper zyRecycleMapper;

    @Autowired
    private ZyRecycleDao zyRecycleDao;
    /**
     * 跳转到回收站首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "recycle.html";
    }

    /**
     * 获取回收站条目列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) throws Exception {
        List<ZyRecycle> list = zyRecycleDao.select(condition, beginTime, endTime);
        RecycleWarpper recycleWar = new RecycleWarpper(list);
        return super.warpObject(recycleWar);
    }


    /**
     * 恢复条目
     */
    @RequestMapping("/recover")
    @BussinessLog(value = "恢复条目", key = "keyName")
    @Permission
    @ResponseBody
    public Tip recover(ZyRecycle item){
        zyRecycleServiceImpl.recover(item);
        return SUCCESS_TIP;

    }

    /**
     * 删除回收站条目信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(ZyRecycle item) {
        zyRecycleServiceImpl.removeRecycle(item);
        return SUCCESS_TIP;
    }

    /**
     *  清空回收站
     */
    @RequestMapping(value = "/deleteAll")
    @ResponseBody
    public Object delete() throws BussinessException{
        List<ZyRecycle> list = zyRecycleMapper.selectList(null);
        for (ZyRecycle item : list) {
            zyRecycleServiceImpl.removeRecycle(item);
        }
        return SUCCESS_TIP;
    }

}
